import 'package:XinXiZiXun/common/apis/apis.dart';
import 'package:XinXiZiXun/common/components/zdy_modules.dart';
import 'package:XinXiZiXun/common/entitys/entitys.dart';
import 'package:XinXiZiXun/common/global/global.dart';
import 'package:XinXiZiXun/common/utils/utils.dart';
import 'package:XinXiZiXun/common/values/values.dart';
import 'package:XinXiZiXun/gen_a/A.dart';
import 'package:flutter/material.dart';

class LoginLayoutPage extends StatelessWidget {
  bool isSelectedBool = true;
  String telString;
  String secrectString;

  //执行登录
  _handleSignIn(BuildContext context) async {
    if (!Verify.isChinaPhoneLegal(telString)) {
      AlertUtil.fromBottom('请输入正确的手机号！', context);
      return;
    }

    if (secrectString == null) {
      AlertUtil.fromBottom('请输入密码！', context);
      return;
    }

    if (!isSelectedBool) {
      AlertUtil.fromBottom('请同意《使用条款》和《隐私政策》！', context);
      return;
    }

    Map<String, dynamic> params = {
      'phone': telString,
      'password': secrectString,
    };

    UserLoginResponseEntity responseEntity = await UserLoginApi.login(
      params: params,
    );

    if (responseEntity.tag == 1) {
      //保存Token
      _saveHandle(context, responseEntity.result);
    } else {
      AlertUtil.fromBottom(responseEntity.message, context);
    }
  }

  _saveHandle(BuildContext context, UserResult entity) async {
    bool isSuccessBool = await Global.saveProfile(entity);
    if (isSuccessBool) {
      print('保存完成');

      _handleBackOperation(context);
    } else {
      print('保存失败');
    }
  }

  //返回
  _handleBackOperation(BuildContext context) {
    Navigator.of(context).pop();
  }

  // //获取用户信息
  // _handleUserInfo() async {
  //   UserInfoEntity responseEntity = await UserInfoApi.userInfo(params: {});
  // }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomPadding: false,
      appBar: AppBar(
        elevation: 0,
        backgroundColor: AppColors.primaryDefaultColor,
        leading: FlatButton(
          onPressed: () {
            FocusScope.of(context).requestFocus(FocusNode());
            _handleBackOperation(context);
          },
          child: Image.asset(A.assets_images_ic_qihuo_back_white),
        ),
      ),
      // backgroundColor: AppColors.primaryLightGrayColor,
      body: SafeArea(
        child: Container(
          margin: EdgeInsets.only(
            left: duSetWidth(2 * ktDefaultPadding),
            right: duSetWidth(2 * ktDefaultPadding),
          ),
          child: Column(
//          mainAxisAlignment: MainAxisAlignment.center,
            children: [
              SizedBox(
                height: duSetHeight(kfDefaultPadding),
              ),
              Image.asset(
                A.assets_images_SmallLogin,
                width: duSetWidth(4 * kfDefaultPadding),
                height: duSetWidth(4 * kfDefaultPadding),
                fit: BoxFit.fill,
              ),
              SizedBox(
                height: duSetHeight(ktDefaultPadding),
              ),
              Text(
                '信息咨询',
                style: TextStyle(
                    fontSize: duSetScaleFontSize(36.0),
                    fontFamily: 'Avenir',
                    fontWeight: FontWeight.w600,
                    color: AppColors.primaryDarkGrayColor),
              ),
              SizedBox(
                height: duSetHeight(kfDefaultPadding),
              ),
              RoundedInputField(
                height: duSetHeight(knDefaultPadding),
                leftImagename: A.assets_images_ic_passwordlogin_phone,
                hintText: '输入手机号',
                issecrectbool: false,
                maxLeght: 11,
                keyBoardType: TextInputType.number,
                onChanged: (value) {
                  telString = value;
                  print('手机号：${value}');
                },
              ),
              SizedBox(
                height: duSetHeight(ktDefaultPadding),
              ),
              RoundedInputField(
                height: duSetHeight(knDefaultPadding),
                leftImagename: A.assets_images_ic_passwordlogin_password,
                hintText: '输入密码',
                issecrectbool: true,
                keyBoardType: TextInputType.emailAddress,
                onChanged: (value) {
                  secrectString = value;

                  print('密码：${value}');
                },
              ),
              SizedBox(
                height: duSetHeight(ktDefaultPadding),
              ),
              ZdySigleTextButton(
                height: duSetHeight(knDefaultPadding),
                text: '登录',
                style: TextStyle(
                  fontSize: duSetScaleFontSize(32.0),
                  fontFamily: 'fontFamily',
                  fontWeight: FontWeight.w600,
                  color: AppColors.primaryWhiteColor,
                ),
                isHasRadiusBool: false,
                backGroundColor: AppColors.primaryElementColor,
                callback: () {
                  print('登录');

                  FocusScope.of(context).requestFocus(FocusNode());

                  _handleSignIn(context);
                },
              ),
              SizedBox(
                height: duSetHeight(kDefaultPadding),
              ),
              Row(
                children: [
                  FlatButton(
                    color: AppColors.primaryDefaultColor,
                    onPressed: () {
                      print('忘记密码');

                      FocusScope.of(context).requestFocus(FocusNode());
                    },
                    child: Text(
                      '忘记密码',
                      style: TextStyle(
                        fontSize: duSetScaleFontSize(24.0),
                        fontFamily: 'Avenir',
                        color: AppColors.primaryLightGrayColor,
                      ),
                    ),
                  ),
                  Spacer(),
                  FlatButton(
                    color: AppColors.primaryDefaultColor,
                    onPressed: () {
                      print('注册');

                      FocusScope.of(context).requestFocus(FocusNode());
                    },
                    child: Text(
                      '注册',
                      style: TextStyle(
                        fontSize: duSetScaleFontSize(24.0),
                        fontFamily: 'Avenir',
                        color: AppColors.primaryElementColor,
                      ),
                    ),
                  ),
                ],
              ),
              SizedBox(
                height: duSetHeight(knDefaultPadding),
              ),
              ZdyAgreement(
                unSelectedImageString: A.assets_images_ic_switch_unselected,
                selectedImageString: A.assets_images_ic_switch_selected,
                prevNameString: '《使用条款》',
                nextNameString: '《隐私政策》',
                isSelectedBool: isSelectedBool,
                callBackRespone: (value) {
                  FocusScope.of(context).requestFocus(FocusNode());
                  isSelectedBool = value;
                  if (isSelectedBool) {
                    print('是选中');
                  } else {
                    print('否选中');
                  }
                },
              )
            ],
          ),
        ),
      ),
    );
  }
}
